//参数配置-数据库配置 dbset.html
app.controller('dbsetCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('dbset');
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 2.1;
  init();
  //获取/刷新用户列表
  function init() {
    $http.get('/deploy/dbset/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var data = message.data;
          //设置默认值
          for (var i in data.databases)
            if (!data.databases[i].password)
              data.databases[i].password = data.databases[i].username;

          $scope.data = data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //检查密码
  $scope.checkPWD = function (db) {
    if (!db.password || Trim(db.password, true) == '') {
      db.isNull = true;
    } else {
      db.isNull = false;
    }
  };

  //新增用户
  $scope.addFnc = function (add) {
    //其他参数
    if (!add) {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!add.username || add.username == "") {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!add.password || add.password == "") {
      $.showNotice("密码不能为空", "danger");
      return;
    }
    if (!add.passwordC || add.passwordC == "") {
      $.showNotice("确认密码不能为空", "danger");
      return;
    }
    if (add.password != add.passwordC) {
      $.showNotice("两次输入密码不相同", "danger");
      return;
    }

    add.ip = $scope.data.ip;
    add.port = $scope.data.port;
    add.sid = $scope.data.sid;

    $http.post('/deploy/dbset/add', add).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#exampleModal').modal('hide');
          $.showNotice("添加成功");
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //删除用户
  $scope.delFnc = function (del) {
    $http.post('/deploy/dbset/del', del).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice("删除成功");
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //测试
  $scope.testFnc = function (bean) {
    $('#dbset .progress').slideDown(200);
    $('#dbset .alert-success').slideUp(200);
    $http.post('/deploy/dbset/test', bean).then(function (response) {
      if (response.status == 200) {
        $('#dbset .progress').slideUp(200);
        var message = response.data;
        if (message.code == 0) {
          $('#dbset .alert-success').slideDown(200);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //上一步
  $scope.dbsetGoBack = function () {
    $location.path(checkThisLast(1.12).path);
  };

  //下一步
  $scope.submitFnc = function (bean) {
    //检查表单
    for (var i in $scope.data.databases) {
      if ($scope.data.databases[i].isNull) {
        $.showNotice('请完成表单表单填写，密码不允许为空', 'warning');
        return;
      }
    }

    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.post('/deploy/dbset/submit', bean).then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          // 脱机版
          if (isOffline) {
            $location.path('platform');
            return;
          }
          $location.path('installDB');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

});